<?php
/**     
  *
  * Copyright (c) 2009, Persistent Systems Limited
  *
  * Redistribution and use, with or without modification, are permitted 
  *  provided that the following  conditions are met:
  *   - Redistributions of source code must retain the above copyright notice, 
  *     this list of conditions and the following disclaimer.
  *   - Neither the name of Persistent Systems Limited nor the names of its contributors 
  *     may be used to endorse or promote products derived from this software 
  *     without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
/**
 * File path for salesOrdersModel.php
 */
 require_once 'model/salesOrdersModel.php';
/**
 * File path for homePageModel.php
 */
 require_once 'model/homePageModel.php';
/**
 * SalesOrders_Controller class to display sales order list, add order and edit order
 * @access public
 */
class SalesOrders_Controller
{
	/** 
     * Class constructor
	 * @access public
	 */
	public function __construct(){ }
	
	/**
	 * This function to show sales order list with option of product filter
	 * @access public
	 * @return void display sales order list page
	 */
	public function showSalesOrderList()
	{
		$regionList = array();
		$productList = array();
		$salesOrdersList = array();
		$errors = array();
		$isApplicationLevelError = false;
		$viewAllSalesPersonsData = 0;
		if(isset($_GET['viewAll'])){
			$viewAllSalesPersonsData = $_GET['viewAll'];
		}
		try{
			$homePageModelObj = new HomePageModel();
			if($viewAllSalesPersonsData == '1'){
				$regionData = $homePageModelObj->getRegionList();
				if (isset($regionData['error'])){
					$errors[] = $regionData['error'];
				}
				if(isset($regionData['result'])){
					$regionList = $regionData['result'];
				}
			}
			$productData = $homePageModelObj->getProductList();
			if (isset($productData['error'])){
				$errors[] = $productData['error'];
			} 
			if(isset($productData['result'])){
				$productList = $productData['result'];
			}

			$productId = 0;
			$regionId = 0;
			$salesPersonId = $_SESSION['salesPersionID'];
			if(empty($errors)){
				if($_POST){
					$productId = $_POST['lstProduct'];
					$regionId = $_POST['lstRegion'];					
				}
				$salesOrderModelObj = new SalesOrderModel();
				$salesOrderData = $salesOrderModelObj->getSalesOrderData($salesPersonId, $productId, $regionId);
				if (isset($salesOrderData['error'])){
					$errors[] = $salesOrderData['error'];
          $isApplicationLevelError = true;
				}
				if(isset($salesOrderData['result'])){
					$salesOrdersList = $salesOrderData['result'];
				}
			} else {
				$isApplicationLevelError = true;
			}
			
			
		} catch (Exception $e){
			throw ($e);
		}
		if(!$isApplicationLevelError){
			include_once('view/salesOrder.php');
		} else {
			include_once('view/error.php');
		}
		exit;
	}
	/**
	 * This function to show add sales order page
	 * @access public
	 * @return void Display Add new sales page
	 */
	public function showAddNewSalesOrderPage()
	{
		$errors = array();
		$productList = array();
		$productId = 0;
		$quantity = 0;
		$isApplicationLevelError = false;

		try{
			$homePageModelObj = new HomePageModel();
			$productData = $homePageModelObj->getProductList();
			
			if (isset($productData['error'])){
				$errors[] = $productData['error'];
			} 
			if(isset($productData['result'])){
				$productList = $productData['result'];
			}
			if(empty($errors)){
				$salesOrderModelObj = new SalesOrderModel();
				if($_POST){
					$errors = $salesOrderModelObj->validateSalesOrderForm($_POST);
					$productId = $_POST['lstProduct'];
					if(empty($errors)){
						$result = $this->addOrder($_POST);
						if(isset($result['error'])){
							$errors[] = $result['error'];
							$isApplicationLevelError = true;
						} else {
							header('location:index.php?a=2');
						}
					} else {
						$isApplicationLevelError = true;
					}
				}
			} else {
				$isApplicationLevelError = true;
			}

		} catch(Exception $e) {
			throw($e);
		}
		if(!$isApplicationLevelError){
			include_once('view/addsalesorder.php');
		} else {
			include_once('view/error.php');
		}
		exit;
	}
	/**
	 * This function to add sales order to database
	 * @access public
	 * @param array $_POST Form field data like product id, order quantity
	 * @return array $result Contains array of response received from web service
	 */
	public function addOrder($_POST)
	{	
		$result = array();
		try{
			if (isset($_SESSION['salesPersionID'])){
				$salesOrderModelObj = new SalesOrderModel();
				$response = $salesOrderModelObj->addSalesOrder($_POST);
				if(!isset($response['error'])){
					$result['result'] = true;
				} else {
					$result['error'] = $response['error'];
				}
				
			}
		} catch (Exception $e){
			throw($e);
		}
		return $result;
	}
	/**
	 * This function to show edit sales order page
	 * @return void Dispaly Edit Sales Order page
	 */
	public function showEditSalesOrderPage()
	{
		$productList = array();
		$productId = 0;
		$quantity = 0;
		$salesOrderId = 0;
		$errors = array();
		$isApplicationLevelError = false;

		try{
			$salesOrderId = trim($_GET['id']);
			$homePageModelObj = new HomePageModel();
			$productData = $homePageModelObj->getProductList();
			
			if (isset($productData['error'])){
				$errors[] = $productData['error'];
			} 
			if(isset($productData['result'])){
				$productList = $productData['result'];
			}

			$salesOrderModelObj = new SalesOrderModel();
			$salesOrderData = $salesOrderModelObj->getSalesOrderDetails($salesOrderId);
			
			if (isset($salesOrderData['error'])){
				$errors[] = $salesOrderData['error'];
			}
			if(isset($salesOrderData['result'])){
				$salesOrder = $salesOrderData['result'];
			}
			if(empty($errors)){
				if(isset($salesOrderData['result'])){
					
					$salesOrder = $salesOrderData['result'];
					$productId = $salesOrder[0]['productid'];
					$quantity = $salesOrder[0]['orderqty'];
					$salesOrderId  = $salesOrder[0]['salesorderid'];
				}
				if ($_POST){
					$productId = $_POST['lstProduct'];
					$quantity = $_POST['txtProductQty'];
					$errors = $salesOrderModelObj->validateSalesOrderForm($_POST);
					if(empty($errors)){
						$result = $this->updateOrder($_POST);
						if(isset($result['error'])){
							$errors[] = $result['error'];
							$isApplicationLevelError = true;
						} else {
							header('location:index.php?a=2');
						}
					} else {
						$isApplicationLevelError = true;
					}
					
				}
			} else {
				$isApplicationLevelError = true;
			}
			
		} catch(Exception $e) {
			throw($e);
		}
		
		if(!$isApplicationLevelError){
			include_once('view/editsalesorder.php');
		} else {
			include_once('view/error.php');
		}
		exit;
	}
	/**
	 * This function to update sales order
	 * @access public
	 * @param array $_POST Form field data like product id, order quantity, Sales Order Id
	 * @return array $result Result
	 */
	public function updateOrder($_POST)
	{
		$result = array();
		
		try{
			if (isset($_SESSION['salesPersionID'])){
				$salesOrderModelObj = new SalesOrderModel();
				$response = $salesOrderModelObj->updateSalesOrder($_POST);
				if(!isset($response['error'])){
					$result['result'] = true;
				} else {
					$result['error'] = $response['error'];
				}
			}

		} catch (Exception $e){
			throw($e);
		}
		return $result;
	}
}
$salesOrders = new SalesOrders_Controller();
?>